// 道具基类
import { TimeManager } from './TimeManager.js';

export class PowerUp {
    constructor(x, y, type) {
        this.x = x;
        this.y = y;
        this.width = 25;
        this.height = 25;
        this.type = type;
        this.speed = 2;
        this.pulsePhase = 0;
        this.rotationAngle = 0;
        this.collected = false;
        
        // 根据类型设置颜色和图标
        this.setupVisuals();
    }
    
    setupVisuals() {
        switch (this.type) {
            case 'health':
                this.color = '#ff4444';
                this.glowColor = '#ff8888';
                this.symbol = '+';
                break;
            case 'multishot':
                this.color = '#44ff44';
                this.glowColor = '#88ff88';
                this.symbol = '≡';
                break;
            case 'shield':
                this.color = '#4444ff';
                this.glowColor = '#8888ff';
                this.symbol = '◇';
                break;
            case 'firerate':
                this.color = '#ff8800';
                this.glowColor = '#ffaa44';
                this.symbol = '»';
                break;
        }
    }
    
    update(deltaTime) {
        this.y += this.speed * (deltaTime / 16.67); // 基于时间的移动
        this.pulsePhase = TimeManager.updatePhase(this.pulsePhase, deltaTime, 9); // 0.15 * 60 = 9弧度/秒
        this.rotationAngle = TimeManager.updatePhase(this.rotationAngle, deltaTime, 3); // 0.05 * 60 = 3弧度/秒
    }
    
    render(ctx) {
        ctx.save();
        
        // 脉冲效果
        const pulse = 0.8 + Math.sin(this.pulsePhase) * 0.3;
        const glowSize = 20 + Math.sin(this.pulsePhase * 1.5) * 5;
        
        // 外层发光效果
        const gradient = ctx.createRadialGradient(
            this.x + this.width/2, this.y + this.height/2, 0,
            this.x + this.width/2, this.y + this.height/2, glowSize
        );
        gradient.addColorStop(0, this.glowColor + 'aa');
        gradient.addColorStop(0.5, this.glowColor + '44');
        gradient.addColorStop(1, this.glowColor + '00');
        
        ctx.fillStyle = gradient;
        ctx.beginPath();
        ctx.arc(this.x + this.width/2, this.y + this.height/2, glowSize, 0, Math.PI * 2);
        ctx.fill();
        
        // 道具容器（六边形）
        ctx.translate(this.x + this.width/2, this.y + this.height/2);
        ctx.rotate(this.rotationAngle);
        
        // 外框
        ctx.strokeStyle = this.color;
        ctx.lineWidth = 2;
        ctx.fillStyle = this.color + '44';
        
        ctx.beginPath();
        for (let i = 0; i < 6; i++) {
            const angle = (i * Math.PI * 2) / 6;
            const x = Math.cos(angle) * 12;
            const y = Math.sin(angle) * 12;
            if (i === 0) {
                ctx.moveTo(x, y);
            } else {
                ctx.lineTo(x, y);
            }
        }
        ctx.closePath();
        ctx.fill();
        ctx.stroke();
        
        // 内部图标
        ctx.rotate(-this.rotationAngle);
        ctx.fillStyle = this.color;
        ctx.font = 'bold 16px Arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.fillText(this.symbol, 0, 0);
        
        ctx.restore();
    }
    
    isOffScreen(canvasHeight) {
        return this.y > canvasHeight + 50;
    }
}

// 医疗包类
export class HealthPack extends PowerUp {
    constructor(x, y) {
        super(x, y, 'health');
        this.healAmount = 1;
    }
    
    applyEffect(player, game) {
        if (game.lives < 5) { // 最大生命值限制
            game.lives += this.healAmount;
            // 更新Canvas血量UI（新系统）
            if (game.canvasHealthUI) {
                game.canvasHealthUI.heal(this.healAmount);
            }
            // 兼容原有血条管理器（如果存在）
            if (game.healthBarManager) {
                game.healthBarManager.heal(this.healAmount);
            }
            // 特效：绿色治疗光环
            for (let i = 0; i < 15; i++) {
                const particle = new HealParticle(
                    player.x + player.width/2,
                    player.y + player.height/2
                );
                game.particles.push(particle);
            }
            return true;
        }
        return false;
    }
}

// 多发装置类
export class MultiShot extends PowerUp {
    constructor(x, y) {
        super(x, y, 'multishot');
        this.duration = TimeManager.CONSTANTS.MULTISHOT_DURATION; // 10秒（毫秒）
    }
    
    applyEffect(player, game) {
        player.multishotTime = this.duration;
        player.multishotActive = true;
        
        // 特效：绿色能量波
        for (let i = 0; i < 20; i++) {
            const particle = new PowerParticle(
                player.x + player.width/2,
                player.y + player.height/2,
                '#44ff44'
            );
            game.particles.push(particle);
        }
        return true;
    }
}

// 保护罩类
export class Shield extends PowerUp {
    constructor(x, y) {
        super(x, y, 'shield');
    }
    
    applyEffect(player, game) {
        // 给玩家添加1次保护罩充能（最多可以叠加到3次）
        if (player.shieldCharges < 3) {
            player.shieldCharges++;
            player.shieldActive = true; // 激活保护罩视觉效果
            player.shieldPhase = 0; // 重置保护罩相位，从初始大小开始
            
            // 特效：蓝色护盾波
            for (let i = 0; i < 25; i++) {
                const particle = new ShieldParticle(
                    player.x + player.width/2,
                    player.y + player.height/2
                );
                game.particles.push(particle);
            }
            
            console.log(`保护罩充能成功！当前充能: ${player.shieldCharges}/3`);
            return true;
        }
        
        console.log('保护罩充能已满！');
        return false; // 充能已满，无法拾取
    }
}

// 治疗粒子效果
export class HealParticle {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.vx = (Math.random() - 0.5) * 4;
        this.vy = (Math.random() - 0.5) * 4 - 2;
        this.life = TimeManager.CONSTANTS.HEAL_PARTICLE_LIFE; // 使用毫秒
        this.maxLife = TimeManager.CONSTANTS.HEAL_PARTICLE_LIFE;
        this.size = Math.random() * 3 + 2;
        this.color = '#44ff44';
    }
    
    update(deltaTime) {
        this.x += this.vx * (deltaTime / 16.67); // 基于时间的位置更新
        this.y += this.vy * (deltaTime / 16.67);
        this.vy -= 0.1 * (deltaTime / 16.67); // 重力
        this.life = TimeManager.updateTimer(this.life, deltaTime);
        this.size *= Math.pow(0.98, deltaTime / 16.67);
    }
    
    render(ctx) {
        const alpha = this.life / this.maxLife;
        ctx.fillStyle = this.color + Math.floor(alpha * 255).toString(16).padStart(2, '0');
        ctx.fillRect(this.x - this.size/2, this.y - this.size/2, this.size, this.size);
    }
}

// 能量粒子效果
export class PowerParticle {
    constructor(x, y, color) {
        this.x = x;
        this.y = y;
        this.color = color;
        const angle = Math.random() * Math.PI * 2;
        const speed = Math.random() * 6 + 2;
        this.vx = Math.cos(angle) * speed;
        this.vy = Math.sin(angle) * speed;
        this.life = TimeManager.CONSTANTS.POWER_PARTICLE_LIFE; // 使用毫秒
        this.maxLife = TimeManager.CONSTANTS.POWER_PARTICLE_LIFE;
        this.size = Math.random() * 4 + 3;
    }
    
    update(deltaTime) {
        this.x += this.vx * (deltaTime / 16.67);
        this.y += this.vy * (deltaTime / 16.67);
        this.vx *= Math.pow(0.95, deltaTime / 16.67);
        this.vy *= Math.pow(0.95, deltaTime / 16.67);
        this.life = TimeManager.updateTimer(this.life, deltaTime);
        this.size *= Math.pow(0.97, deltaTime / 16.67);
    }
    
    render(ctx) {
        const alpha = this.life / this.maxLife;
        ctx.save();
        ctx.globalAlpha = alpha;
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
        ctx.fill();
        ctx.restore();
    }
}

// 射击频率提升道具类（永久效果）
export class FireRateBooster extends PowerUp {
    constructor(x, y) {
        super(x, y, 'firerate');
        this.fireRateIncrease = 0.8; // 射击冷却时间减少20%（0.8倍）
    }
    
    applyEffect(player, game) {
        // 永久减少射击冷却时间（最低限制为50毫秒）
        const newCooldown = Math.max(50, game.shootCooldown * this.fireRateIncrease);
        game.shootCooldown = newCooldown;
        
        // 特效：橙色闪电能量波
        for (let i = 0; i < 30; i++) {
            const particle = new FireRateParticle(
                player.x + player.width/2,
                player.y + player.height/2
            );
            game.particles.push(particle);
        }
        
        console.log(`射击频率提升！新的射击冷却时间: ${newCooldown}ms`);
        return true;
    }
}

// 射击频率提升粒子效果
export class FireRateParticle {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.color = '#ff8800';
        const angle = Math.random() * Math.PI * 2;
        const speed = Math.random() * 8 + 4; // 更快的速度
        this.vx = Math.cos(angle) * speed;
        this.vy = Math.sin(angle) * speed;
        this.life = TimeManager.CONSTANTS.POWER_PARTICLE_LIFE; // 使用毫秒
        this.maxLife = TimeManager.CONSTANTS.POWER_PARTICLE_LIFE;
        this.size = Math.random() * 5 + 3; // 稍大的粒子
        this.sparkle = Math.random() * Math.PI * 2; // 闪烁效果
    }
    
    update(deltaTime) {
        this.x += this.vx * (deltaTime / 16.67);
        this.y += this.vy * (deltaTime / 16.67);
        this.vx *= Math.pow(0.92, deltaTime / 16.67); // 更慢的减速
        this.vy *= Math.pow(0.92, deltaTime / 16.67);
        this.life = TimeManager.updateTimer(this.life, deltaTime);
        this.size *= Math.pow(0.96, deltaTime / 16.67); // 更慢的缩小
        this.sparkle += 0.3 * (deltaTime / 16.67); // 闪烁动画
    }
    
    render(ctx) {
        const alpha = this.life / this.maxLife;
        const sparkleAlpha = (Math.sin(this.sparkle) + 1) * 0.5; // 0-1之间
        
        ctx.save();
        ctx.globalAlpha = alpha * sparkleAlpha;
        
        // 绘制闪电形状的粒子
        ctx.fillStyle = this.color;
        ctx.strokeStyle = '#ffcc00';
        ctx.lineWidth = 1;
        
        // 创建闪电形状
        ctx.beginPath();
        ctx.moveTo(this.x - this.size, this.y);
        ctx.lineTo(this.x + this.size * 0.3, this.y - this.size * 0.5);
        ctx.lineTo(this.x - this.size * 0.3, this.y - this.size * 0.3);
        ctx.lineTo(this.x + this.size, this.y);
        ctx.lineTo(this.x - this.size * 0.3, this.y + this.size * 0.5);
        ctx.lineTo(this.x + this.size * 0.3, this.y + this.size * 0.3);
        ctx.closePath();
        
        ctx.fill();
        ctx.stroke();
        
        ctx.restore();
    }
}

// 护盾粒子效果
export class ShieldParticle {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.color = '#4444ff';
        const angle = Math.random() * Math.PI * 2;
        const speed = Math.random() * 5 + 3;
        this.vx = Math.cos(angle) * speed;
        this.vy = Math.sin(angle) * speed;
        this.life = TimeManager.CONSTANTS.SHIELD_PARTICLE_LIFE; // 使用毫秒
        this.maxLife = TimeManager.CONSTANTS.SHIELD_PARTICLE_LIFE;
        this.size = Math.random() * 3 + 2;
        this.rotation = 0;
    }
    
    update(deltaTime) {
        this.x += this.vx * (deltaTime / 16.67);
        this.y += this.vy * (deltaTime / 16.67);
        this.vx *= Math.pow(0.94, deltaTime / 16.67);
        this.vy *= Math.pow(0.94, deltaTime / 16.67);
        this.life = TimeManager.updateTimer(this.life, deltaTime);
        this.rotation += 0.2 * (deltaTime / 16.67);
        this.size *= Math.pow(0.98, deltaTime / 16.67);
    }
    
    render(ctx) {
        const alpha = this.life / this.maxLife;
        ctx.save();
        ctx.translate(this.x, this.y);
        ctx.rotate(this.rotation);
        ctx.globalAlpha = alpha;
        ctx.fillStyle = this.color;
        
        // 绘制六边形
        ctx.beginPath();
        for (let i = 0; i < 6; i++) {
            const angle = (i * Math.PI * 2) / 6;
            const x = Math.cos(angle) * this.size;
            const y = Math.sin(angle) * this.size;
            if (i === 0) {
                ctx.moveTo(x, y);
            } else {
                ctx.lineTo(x, y);
            }
        }
        ctx.closePath();
        ctx.fill();
        
        ctx.restore();
    }
}